package com.frxuexi.park2303.user

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import androidx.activity.viewModels
import androidx.lifecycle.lifecycleScope
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.frxuexi.base.mvi.BaseActivity
import com.frxuexi.park2303.R
import com.frxuexi.park2303.databinding.ActivityRegisterBinding
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import java.util.Random
import java.util.Timer
import kotlin.concurrent.timerTask

@Route(path = "/app/RegisterActivity")
@AndroidEntryPoint
class RegisterActivity : BaseActivity<ActivityRegisterBinding,UserIntent,UserViewModel>() {

    var timer:Timer?=null
    var num=10
    var code=0
    override fun getVM(): UserViewModel {
        return viewModels<UserViewModel>().value
    }

    override fun getBind(): ActivityRegisterBinding {
        return ActivityRegisterBinding.inflate(layoutInflater)
    }

    override fun initData() {
        vm.registerData.observe(this){
            if(it!=null) {
                Log.i("lr", it.toString())
                ToastUtils.showLong("注册成功")
//                ARouter.getInstance().build("/app/LoginActivity").withString("uname",it.username).withString("pass",it.password).navigation()
//                finish()
            }
        }
        vm.registerError.observe(this){
            Log.i("lr",it.toString())
            ToastUtils.showLong(it)
        }
    }

    override fun initView() {
        binding.registerSendcode.apply {
            setOnClickListener {
                code = (1000..9999).random()
                ToastUtils.showLong(code.toString())
                isEnabled=false
                lifecycleScope.launch(Dispatchers.IO) {
                    timer=Timer()
                    timer!!.schedule(timerTask {
                        lifecycleScope.launch(Dispatchers.Main) {
                            setText("还剩${num}s")
                            if(num==0){
                                isEnabled=true
                                setText("重新发送")
                                timer?.cancel()
                                num=10
                            }
                        }
                        num--
                    },0,1000)
                }
            }
        }

        binding.btnRegister.setOnClickListener {
            val uname = binding.registerUname.text.toString()
            val pass = binding.registerPass.text.toString()
            ARouter.getInstance().build("/app/LoginActivity").withString("uname",uname).withString("pass",pass).navigation()
            finish()

//
//            val pass2 = binding.registerPass2.text.toString()
//            val code = binding.registerCode.text.toString()
//            if(uname.isNullOrEmpty() || pass.isNullOrEmpty() || pass2.isNullOrEmpty() || code.isNullOrEmpty()){
//                ToastUtils.showLong("输入内容不能为空")
//                return@setOnClickListener
//            }
//            //1[0-9]{10}
//            if(!uname.matches(Regex("[A-z0-9_]{2,8}"))){
//                ToastUtils.showLong("不是合法账号")
//                return@setOnClickListener
//            }
//            if(pass!=pass2){
//                ToastUtils.showLong("密码不一致")
//                return@setOnClickListener
//            }
//            if(this.code.toString()!=code){
//                ToastUtils.showLong("验证码错误")
//                return@setOnClickListener
//            }
//            vm.sendIntent(UserIntent.register(uname,pass))
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        if(timer!=null){
            timer!!.cancel()
            timer=null
        }
    }

}